package com.yonyou.ctm.ctmplugin.utils;


import com.intellij.codeInsight.hint.HintManager;
import com.intellij.notification.*;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.MessageDialogBuilder;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.ui.components.JBLabel;
import com.intellij.util.Consumer;
import com.yonyou.component.SparrowProject;
import com.yonyou.utils.SparrowInterface;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 提示语工具类
 *
 * @author fengjiening
 * @since 2023-02-28
 */
public class NotificationUtil {
    static final String disId="com.fengjiening.notify";



    public static void showDialog(String title,String mess){
        //Project project = e.getData(PlatformDataKeys.PROJECT);
        SparrowProject app = ApplicationManager.getApplication().getComponent(SparrowProject.class);
        // 弹出模态框显示以下的消息
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Messages.showMessageDialog(mess, title, Messages.getInformationIcon());
            }
        });
    }
    public static void showErrDialog(String title,String mess){
        //Project project = e.getData(PlatformDataKeys.PROJECT);
        SparrowProject app = ApplicationManager.getApplication().getComponent(SparrowProject.class);
        // 弹出模态框显示以下的消息
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Messages.showMessageDialog(mess, title, Messages.getErrorIcon());
            }
        });
    }
    public static void showNotification(@NotNull NotificationType type, String mess){
        NotificationGroup notificationGroup = new NotificationGroup(disId, NotificationDisplayType.BALLOON, true);
        Notification notification = notificationGroup.createNotification(mess,type);
        Notifications.Bus.notify(notification);
    }

  /**
   * 添加监听事件 IPopupChooserBuilder<T> setItemChosenCallback(@NotNull Consumer<? super T> var1);
   * 设置可拖动大小 IPopupChooserBuilder<T> setResizable(boolean var1);
   * 设置窗体是否可拖动 IPopupChooserBuilder<T> setMovable(boolean var1);
   * 设置弹框标题 IPopupChooserBuilder<T> setTitle(@NotNull @PopupTitle String
   * var1);
   *
   * @param title
   * @param data
   * @param func
   */
  public void showBoxPopup(String title, String[] data, SparrowInterface<String> func) {
        List<String> list = Arrays.asList(data);
        JBPopupFactory instance = JBPopupFactory.getInstance();// 创建实例
        instance.createPopupChooserBuilder(list)
                .setTitle(title)// 设置标题
                .setItemChosenCallback(new Consumer<String>() {// 添加监听事件
                    @Override
                    public void consume(String s) {
                        func.apply(s);
                    }
                }).createPopup().showInFocusCenter();// 在屏幕中间显示
    }

    /**
     * 创建窗口
     */
    public void showPage(String title,String buttonName, SparrowInterface<ActionEvent> func) {
        JBPopupFactory instance = JBPopupFactory.getInstance();//创建JBPopupFactory实例
        JButton button = new JButton(buttonName);//新建Button放到自定义弹框中
        JPanel panel = new JPanel();
        panel.setSize(40,20);//限制Buton大小，否则会填充整个弹框
        panel.add(button);
        button.addActionListener(d ->func.apply(d));// 添加监听事件
        instance.createComponentPopupBuilder(panel, new JBLabel())//参数说明：内容对象,优先获取
                .setTitle(title)
                .setMovable(true)
                .setResizable(true)
                .setNormalWindowLevel(false)
                .setMinSize(new Dimension(600,300))
                .createPopup()
                .showInFocusCenter();
    }


    //public static void MessageHint(){
    //    HintManager.getInstance().showErrorHint(CommonDataKeys.EDITOR.getData(e.getDataContext()),"错误信息");
    //}
}
